Ext.define('YXMES.smat.controller.MergeMaterialSnController', {
	extend : 'Ext.app.Controller',
	refs : [ {
		selector : 'smat_mergematerialsn',
		ref : 'smatmergematerialsn'
	} ],
	requires : [  ],
	stores : [],
	models : [],
	views : [ 'YXMES.smat.view.MergeMaterialSnView' ],
	subcontrollers : [  ],
	_init : false,
	init : function() {
		if (this._init)
			return;
		this._init = true;
		this.control({
			'smat_mergematerialsn' : {
				afterrender : function() {
					return this.handle(this.onAfterrender, arguments);
				},
				destroy : function() {
					return this.handle(this.onDestroy, arguments);
				}
			},
			'smat_mergematerialsn #txtFromMsn' : {
				specialkey : function() {
					return this.handle(this.onTxtFromMsnSpecialkey, arguments);
				}
			},
			'smat_mergematerialsn #txtToMsn' : {
				specialkey : function() {
					return this.handle(this.onTxtToMsnSpecialkey, arguments);
				}
			},
			'smat_mergematerialsn #btnMerge' : {
				click : function() {
					return this.handle(this.onBtnMergeClick, arguments);
				}
			}
		});
		if (!Ext.isEmpty(this.subcontrollers)) {
			Ext.each(this.subcontrollers, function(name) {
				this.getController(name).init();
			}, this);
		}
		// User definied init
	},
	mainView : undefined,
	getMainView : function() {
		return this.mainView;
	},
	getScopeView : function() {
		return this.getMainView().scopeView || {};
	},
	getInput : function() {
		return this.getScopeView().input || {};
	},
	setInput : function(data) {
		this.getMainView().input = data;
	},
	getOutput : function() {
		return this.getMainView().output || {};
	},
	setOutput : function(data) {
		this.getScopeView().output = data;
	},
	handle : function(func, args) {
		var item = args && args.length > 0 && Ext.typeOf(args[0]) === 'object' ? args[0] : undefined;
		if (item) {
			try {
				this.mainView = (item.xtype == 'smat_mergematerialsn' ? item : item.up('smat_mergematerialsn'));
			} catch(e) {
			}
		}
		try {
			return func.apply(this, args);
		} finally {
		}
	},
	getItem : function(itemId) {
		return this.getMainView().down('#' + itemId);
	},
	getStore : function(name) {
		return undefined;
	},
	bind : function(view) {
		view = (view ? view : this.getMainView());
	},
	onAfterrender : function(view, eOpts) {
		this.bind();
		// User defined afterrender
	},
	onDestroy : function(view, eOpts) {
		// User defined destory
		var view = this.getMainView();
		if (this.getMainView().input)
			this.getMainView().input = undefined;
		if (this.getMainView().output)
			this.getMainView().output = undefined;
	},
	onTxtFromMsnSpecialkey : function(textfield, e, eOpts) {
		if (e.getKey() == e.ENTER && textfield.getValue() !== '') {
		    this.loadMsn('F', textfield.getValue());
		}
	},
	onTxtToMsnSpecialkey : function(textfield, e, eOpts) {
		if (e.getKey() == e.ENTER && textfield.getValue() !== '') {
		    this.loadMsn('T', textfield.getValue());
		}
	},
	onBtnMergeClick : function(button, e, eOpts) {
		var frm = this.getMainView();
		if (!frm.isValid()) {
		    return;
		}
		
		var me = this;
		Ext.Msg.show({
		    title: button.getText(),
		    msg: T.msg.are_you_sure_to_merge_material_sn,
		    icon: Ext.Msg.QUESTION,
		    buttons: Ext.Msg.YESNO,
		    fn: function(btnId) {
		        if ('yes' == btnId) {
		            button.setDisabled(true);
		
		            YX.ZbusRpc.request({
		                module: 'SmatMergeMaterialSnService',
		                params: {
		                    fromMsn: me.getItem('txtFromMsn').getValue(),
		                    toMsn: me.getItem('txtToMsn').getValue(),
		                    moveQty: me.getItem('numMoveQty').getValue()
		                },
		                showSuccess: true,
		                callback: function() {
		                    button.setDisabled(false);
		                },
		                success: function() {
		                    frm.reset();
		                    me.getItem('txtFromMsn').focus();
		                }
		            });
		        }
		    }
		})
	},
	loadMsnCompleted : function(p, msnData) {
		if (p === 'F') {
		    this.getItem('txtFromWhCode').setValue(msnData.whCode);
		    this.getItem('txtFromQty').setValue(msnData.qty);
		    this.getItem('numMoveQty').setValue(msnData.qty);
		    this.getItem('txtFromMatCode').setValue(msnData.matCode);
		    this.getItem('txtFromMatVer').setValue(msnData.matVer);
		    this.getItem('txtFromMatDesc').setValue(msnData.matDesc);
		    this.getItem('txtToMsn').focus(true);
		} else if (p === 'T') {
		    this.getItem('txtToStoreCode').setValue(msnData.whCode);
		    this.getItem('txtToQty').setValue(msnData.qty);
		    this.getItem('txtToMatCode').setValue(msnData.matCode);
		    this.getItem('txtToMatVer').setValue(msnData.matVer);
		    this.getItem('txtToMatDesc').setValue(msnData.matDesc);
		}
	},
	loadMsn : function(p, vMsn) {
		if (Ext.isEmpty(vMsn)) {
		    return;
		}
		
		var me = this;
		YX.ZbusRpc.request({
		    module: 'SmatViewMaterialSnService',
		    params: {
		        msn: vMsn
		    },
		    success: function(rsp) {
		        if (!Ext.isEmpty(rsp.msnList)) {
		            me.loadMsnCompleted(p, rsp.msnList[0]);
		        }
		    }
		});
	}
});